import { loginApi, registerApi,getUserAuthTypes,getloginText} from '@/common/loginApi'
import { UserProps, loginAndRes,LoginTextType } from '@/common/utils/loginTpye'
import router from '@/router'
import axios from 'axios'
import { ElMessage, ElMessageBox } from 'element-plus'
import { defineStore } from 'pinia'
import { reactive } from 'vue'
// import { useDialogStore } from "@/store/dialogStore";
// const dialogStore = useDialogStore();
// 定义登录对象
export const logindata: loginAndRes = reactive({
  phone: "",
  smsCode: "", //验证码
});
// 定义注册对象
export const registerdata: loginAndRes = reactive({
  name: "",
  phone: "",
  smsCode: "", //验证码
  agencyCode: "", //经销商代码
  province: '', //省
  city: "", //市
});
export const useUserStore = defineStore('user', {
    state: (): UserProps => {
      return {
        token: localStorage.getItem('token') || '',
        isLogin: localStorage.getItem('token') ? true:false,
        useInfo: null,
        isLayer: false,
        logindata: logindata,
        registerdata: registerdata,
        loginTxt:null as any
      }
    },
  actions: {
      showDialog() {
        this.isLayer = true;
      },
      hideDialog() {
        this.isLayer = false;
      },
      async loginFunction(obj:loginAndRes) {
        loginApi(obj.phone,obj.smsCode).then((res:any) => {
          // console.log(res.data);  
          if (res.code == 200) {
            const { token } = res.data 
            localStorage.setItem('token', token)
            localStorage.setItem('userInfo', JSON.stringify(res.data.unserInfo))
            this.useInfo = res.data.unserInfo
            this.isLogin = true
            this.isLayer = false
            // dialogStore.hideDialog()
          }
          
        });
      },
      async registeFunction(obj:loginAndRes) {
        registerApi(obj).then((res) => {
          if (res.code != 200) {
            // this.btnis = false
            ElMessage.error(res.msg)
            
          }else{
            // console.log(res.data);
            // isConditionMet(true)
            // this.btnis = true
            ElMessage("注册成功，请登录")
          }
        });
      },
    logout(path?:any) {
      ElMessageBox.confirm(
        '您确定要退出登录吗？',
        '提示',
      
        {
          confirmButtonText: '是的',
          cancelButtonText: '取消',
          type: 'warning',
          confirmButtonClass: 'ExitConfirmButton'
        }
      )
        .then(() => {
          this.token = ''
          this.isLogin = false
          localStorage.removeItem('token')
          localStorage.removeItem('userInfo')
          delete axios.defaults.headers.common.Authorization
          if (path.includes('/user')) {
            router.push('/')
            
          } else {
            router.push(path)
          }
          
          ElMessage({
            type: 'success',
            message: '已退出登录',
          })
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: 'Delete canceled',
          })
        })    
       
        
    },
    // 判断token是否过期
      async  getUserAuth() {
        getUserAuthTypes().then((res) => {
          if (res.code != 200) {
            this.token = ''
            this.isLogin = false
            localStorage.removeItem('token')
            localStorage.removeItem('userInfo')
            delete axios.defaults.headers.common.Authorization  
          } 
        });
    },
    // 获取登录框文字
    getLoginTxt() {
      getloginText().then((result) => {
        // console.log(result)
        this.loginTxt = result.data as unknown as LoginTextType
      });
      }
    }
  })
export function isConditionMet(value: any): boolean {
    // 判断逻辑
    return value; // 假设条件已满足
}